package com.itheima.action;

import com.itheima.domain.Customer;
import com.itheima.domain.User;
import com.itheima.domain.Visit;
import com.itheima.service.CustomerService;
import com.itheima.service.VisitService;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Set;

/**
 * Created by Mr.King on 2017/2/26.
 */
public class VisitAction implements ModelDriven<Visit>{

    private VisitService service;
    public void setService(VisitService service) {
        this.service = service;
    }
    
    private CustomerService customerService;
    public void setCustomerService(CustomerService customerService) {
        this.customerService = customerService;
    }

    private List<User> users;
    private List<Customer> customers;
    private List<Visit> visits;

    public List<User> getUsers() {
        return users;
    }
    public List<Customer> getCustomers() {
        return customers;
    }
    public List<Visit> getVisits() {
        return visits;
    }

    private Visit visit = new Visit();
    @Override
    public Visit getModel() {
        return visit;
    }
    public Visit getVisit() {
        return visit;
    }

    //跳转添加访问记录页面
    public String add(){
        users = service.findUser();
        customers = service.findCustomer();
        return "add";
    }

    //保存添加
    public String saveAdd(){
        service.saveAdd(visit);
        return "toList";
    }

    //显示客服拜访列表
    public String list(){
        visits = service.list();
        return "list";
    }

    //跳转修改页面
    public String edit(){
        users = service.findUser();
        customers = service.findCustomer();
        visit = service.findOne(visit);
        return "edit";
    }

    //保存修改
    public String saveEdit(){
        service.saveEdit(visit);
        return "toList";
    }

    //删除拜访记录
    public String delete(){
        service.delete(visit);
        return "toList";
    }

    //模糊查询
    public String likeSerch(){
        //为了页面回显效果，这里我们选择手动封装数据
        HttpServletRequest request = ServletActionContext.getRequest();
        User user = new User();
        user.setUsername(request.getParameter("username"));
        visit.setUser(user);

        visits = service.findSerch(visit);
        return "list";
    }
}
